package org.example.controller;

import org.example.pojo.Doctor.*;
import org.example.pojo.Result;
import org.example.service.AdminUserService;
import org.example.service.HospitalService;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RestController
@RequestMapping("/admin/hospitals")
public class HospitalController {
    //private static final Logger logger = LoggerFactory.getLogger(HospitalController.class);
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private UserService userService;
    @Autowired
    private AdminUserService adminUserService;
    //获取医院
    @GetMapping
    public Result getAllHospitals() {
        List<Hospital> hospitals = hospitalService.getAllHospitals();
        return Result.success(hospitals);
    }
    //获取部门
    @GetMapping("/departments")
    public Result getDepartmentsByHospitalId(Integer hospitalId) {
        if(hospitalId==null){
            List<Department> departments1 = hospitalService.getAllDepartment();
            return Result.success(departments1);
        }else{
            List<Department> departments = hospitalService.getDepartmentsByHospitalId(hospitalId);
            return Result.success(departments);
        }
    }
    //获取科室门诊
    @GetMapping("/clinics")
    public Result getClinicsByDepartmentId(Integer departmentId) {
        if(departmentId==null){
            List<OutpatientClinic> clinics1 = hospitalService.getAllClinics();
            return Result.success(clinics1);
        }else {
            List<OutpatientClinic> clinics = hospitalService.getClinicsByDepartmentId(departmentId);
            return Result.success(clinics);
        }

    }
    //获取医生
    @GetMapping("/doctors")
    public Result getDoctorsByClinicId(Integer clinicId) {
        if (clinicId==null){
            List<Doctor> doctors1 = hospitalService.getAllDoctors();
            return Result.success(doctors1);
        }
        List<Doctor> doctors = hospitalService.getDoctorsByClinicId(clinicId);
        return Result.success(doctors);
    }
    //增加医院
    @PostMapping
    public Result addHospital(@RequestBody @Validated Hospital hospital) {
        hospitalService.addHospital(hospital);
        return Result.success();
    }
    //增加科室
    @PostMapping("/departments")
    public Result addDepartment(@RequestBody @Validated Department department) {
        hospitalService.addDepartment(department);
        return Result.success();
    }
    //增加门诊
    @PostMapping("/clinics")
    public Result addClinic(@RequestBody @Validated OutpatientClinic clinic) {
        hospitalService.addClinic(clinic);
        return Result.success();
    }
    //增加医生
    @PostMapping("/doctors")
    public Result addDoctor(@RequestBody @Validated Doctor doctor) {
        //测试：检测返回的数据是否成功
        //logger.info("Received doctor data: {}", doctor);
        String username = doctor.getUsername();
        if (username == null) {
            return Result.error("用户名不能为空");
        }
        // 假设 hospitalService 有一个方法用于根据 username 检查医生是否存在
        Doctor d = hospitalService.findByUserName(username);
        if (d==null) {
            // 如果医生不存在，调用服务层方法添加医生
            hospitalService.addDoctor(doctor);
            userService.register(username,"123456","2","医生");
            return Result.success("医生添加成功");
            //如果医生不存在还应该给医生分配初始密码123456
        }else{
            // 如果医生已存在，返回失败结果
            return Result.error("该用户名对应的医生已存在，无法重复添加");
        }
    }
    //删除医院信息
    @DeleteMapping
    public Result deleteHospital(Integer hospitalId) {
        hospitalService.deleteHospital(hospitalId);
        return Result.success("删除成功");
    }
    //删除部门信息
    @DeleteMapping("/departments")
    public Result deleteDepartment(Integer departmentId) {
        hospitalService.deleteDepartment(departmentId);
        return Result.success("删除成功");
    }
    //删除科室信息
    @DeleteMapping("/clinics")
    public Result deleteClinic(Integer clinicId) {
        hospitalService.deleteClinic(clinicId);
        return Result.success("删除成功");
    }
    //删除医生信息
    @DeleteMapping("/doctors")
    public Result deleteDoctor(Integer doctorId) {
        String username = hospitalService.findByID(doctorId);
        List<Schedule> schedules = adminUserService.getDoctorScheduleNull();
        //通过doctorId找到schedule的Id
        schedules.forEach(schedule -> {
            if (schedule.getDoctorId()==doctorId){
                adminUserService.deleteDoctorSchedule(schedule.getScheduleId());
            }
        });
        hospitalService.deleteDoctor(doctorId);
        userService.deleteUser(username);

        return Result.success("删除成功");
    }
    //更新医院信息
    @PutMapping
    public Result updateHospital(@RequestBody @Validated Hospital hospital) {
        hospitalService.updateHospital(hospital);
        return Result.success("更新成功");
    }
    //所有部门信息
    @PutMapping("/updateDepartments")
    public Result updateDepartment(@RequestBody @Validated Department department) {
        hospitalService.updateDepartment(department);
        return Result.success("更新");
    }
    //更新科室信息
    @PutMapping("/updateClinics")
    public Result updateClinic(@RequestBody @Validated OutpatientClinic clinic) {
        hospitalService.updateClinic(clinic);
        return Result.success("更新成功");
    }
}
